package com.quanyue.mechanics.complex.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.mechanics.complex.mapper.PurposeMapper;
import com.quanyue.mechanics.complex.service.PurposeService;
import com.quanyue.mechanics.domain.entity.Purpose;
import com.vhuomai.mybatis.mapper.page.Page;
import com.vhuomai.mybatis.mapper.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class PurposeServiceImpl implements PurposeService {
    @Autowired
    private PurposeMapper purposeMapper;

    @Override
    public Purpose addPurpose(Purpose purpose) {
        purpose.setCreateTime(LocalDateTime.now());
        purpose.setUpdateTime(LocalDateTime.now());
        purposeMapper.insert(purpose);
        return purpose;
    }

    @Override
    public void updatePurpose(Purpose purpose) {
        Purpose check = purposeMapper.findOne(purpose.getId());
        if (check == null) throw new AssertUtils.ValidateException("error purpose check");
        purpose.setCreateTime(check.getCreateTime());
        purpose.setUpdateTime(LocalDateTime.now());
        purposeMapper.updateSelective(purpose);
    }

    @Override
    public void deletePurpose(Integer id) {
        Purpose check = purposeMapper.findOne(id);
        if (check == null) throw new AssertUtils.ValidateException("error purpose check");
        purposeMapper.deleteOne(id);
    }

    @Override
    public Page <Purpose> findPurpose(PageRequest request) {
        return purposeMapper.findLimit(request);
    }
}
